<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 已注册的用户列表 即只有这些用户可以登录
      let loginList = [
        { userName: 'admin', password: 'admin321' },
        { userName: 'zhangsan', password: 'zs321' },
        { userName: 'lisi', password: 'ls321' },
        { userName: 'yyqx', password: 'yyqx321' },
      ]
      // 现有的用户信息列表
      // 根据生成的token 去掉找到 对应的信息
      let userInfo = {
        token1: {
          city: '杭州',
          money: '1000w',
          sex: '女',
          userName: 'admin',
          password: 'admin321',
        },
        token2: {
          city: '北京',
          money: '1100w',
          sex: '女',
          userName: 'zhangsan',
          password: 'zs321',
        },
        token3: {
          city: '北京',
          money: '800w',
          sex: '男',
          userName: 'lisi',
          password: 'ls321',
        },
        token4: {
          city: '天津',
          money: '600w',
          sex: '男',
          userName: 'yyqx',
          password: 'yyqx321',
        },
      }

      function register(loginList, userInfo) {
        let count = 2
        for (let i = 0; i < loginList.length; i++) {
          let userName1 = prompt('请输入用户名')
          let password1 = prompt('请输入密码')

          if (count) {
            if (
              userName1 === loginList[i].userName &&
              password1 === loginList[i].password
            ) {
              let num = `token${Math.floor(Math.random() * 4) + 1}`
              for (let key in userInfo) {
                // console.log((key));
                //登录成功
                if (num === key) {
                  userInfo[key].userName = userName1
                  delete userInfo[key].password
                  // alert(`登录成功，具体用户信息：
                  // ${userInfo[key]}
                  // `)
                  alert(`登录成功，具体用户信息：
                city: ${userInfo[key].city}
                money: ${userInfo[key].money}
                sex: ${userInfo[key].sex}
                userName: ${userInfo[key].userName}
                `)

                  // count--
                  // if (count === 0) {
                  //   alert('再次输错，没机会了，直接退出')
                  //   break
                  // }
                }
              }
              break
              //登录失败
            } else {
              let action = +prompt(`
                账户不存在：
                1、注册账户
                2、直接退出
                `)
              switch (action) {
                case 1:
                  let userName2 = prompt('请输入用户名')
                  let password2 = prompt('请输入密码')
                  loginList.push( { userName: userName2, password: password2 }) 
                  alert('你已经注册成功，直接去登录')
                  console.log(loginList)
                  break
                case 2:
                  break
                default:
                  break
              }
              
              count--
            }
             console.log(loginList);
          }
        }
        if (count === 0) {
          alert('再次输错，没机会了，直接退出')
          return
        }
      }
      register(loginList, userInfo)
    </script>
  </body>
</html>
